package org.vs.controller;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import org.vs.service.SSEService;

import javax.annotation.Resource;

@RestController
@RequestMapping("/sse")
public class SSEController {
    @Resource
    private SSEService sseService;

    @GetMapping(value = "/createSseConnect", produces = "text/event-stream;charset=UTF-8")
    public SseEmitter createSseConnect(@RequestParam(name = "id") String id) {
        return sseService.connect(id);
    }


    @PostMapping("/sendMessage")
    public void sendMessage(@RequestParam("id") String id, @RequestParam("message") String message) {
        sseService.sendMessage(id, message);
    }

    @GetMapping("/closeSseConnect")
    public void closeSseConnect(String id) {
        sseService.delete(id);
        return;
    }
}
